// pages/completed/completed.js
import {http} from '../../utils/requestUtil'
Page({
  data: {
    completedNotes: [{
      title:"1",
      content:"1",
      time:"1",
      importance:"high"
    }],
    completedCount: 0
  },
  async break(e){
    wx.showModal({
      title: '提示',
      content: '重新启动该标签',
      success: async(res) => {
        if (res.confirm) {
          const currentId = e.currentTarget.dataset.id
          const res = await http.put("/memo/note",{noteId:currentId,delFlag:'0'})
          this.loadData()
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },
  async delete(e){
    wx.showModal({
      title: '提示',
      content: '删除该便签',
      success: async(res) => {
        if (res.confirm) {
          const currentId = e.currentTarget.dataset.id
          const res = await http.delete("/memo/note/"+currentId,)
          this.loadData()
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },
  onLoad() {
    this.loadData()
    // this.loadCompletedNotes();
  },
  async loadData(){
    const res =await http.get("/memo/note/userlist",{
      delFlag:'1'
    })
    console.log(res.rows)
    const completedNotes = res.rows;
    this.setData({
      completedNotes,
      completedCount: completedNotes.length
    });
  },
  // 加载已完成便签
  loadCompletedNotes() {
    const allNotes = wx.getStorageSync('notes') || [];
    const completedNotes = allNotes.filter(note => note.completed);
    
    this.setData({
      completedNotes,
      completedCount: completedNotes.length
    });
  },

  // 撤销完成状态
  undoComplete(e) {
    const id = e.currentTarget.dataset.id;
    const allNotes = wx.getStorageSync('notes') || [];
    
    const updatedNotes = allNotes.map(note => {
      if (note.id === id) {
        return { ...note, completed: false };
      }
      return note;
    });
    
    wx.setStorageSync('notes', updatedNotes);
    // this.loadCompletedNotes();
    
    wx.showToast({
      title: '已恢复为待办',
      icon: 'success'
    });
  },

  // 删除便签
  deleteNote(e) {
    const id = e.currentTarget.dataset.id;
    const allNotes = wx.getStorageSync('notes') || [];
    
    wx.showModal({
      title: '确认删除',
      content: '删除后无法恢复',
      success: (res) => {
        if (res.confirm) {
          const updatedNotes = allNotes.filter(note => note.id !== id);
          wx.setStorageSync('notes', updatedNotes);
          // this.loadCompletedNotes();
          
          wx.showToast({
            title: '已删除',
            icon: 'success'
          });
        }
      }
    });
  }
});